var appPath = getAppPath();	
var myDomain = 'http://7xogoq.com1.z0.glb.clouddn.com';
var validFile = false;
var uploader = null;
var jsonData = null;

$(function(){
	initJspGrid();
	
	// 初始化 视频播放 点击红叉  停止播放
	$('#playVideoDlg').window({
	       onBeforeClose:function(){ 
	    	   var myVideo = document.getElementsByTagName('video')[0];
	    	   myVideo.pause(); // 暂停视频 播放
	       }
	});
});

// 自定义动画列表初始化
function initJspGrid(){
	$('#videoTmplGrid').datagrid({
	    url:appPath + "/yjxCust/getVideoTmplData.action",
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
		checkbox:false,
	    columns:[[
			{field:'theme_thumb',title:'缩略图',width:70,align:'center',sortable:false,
				formatter:function(value, rowData, rowIndex){
					var retUrl = '';
					if (!value) {
						value = 'http://wx.qlogo.cn/mmopen/ajNVdqHZLLAkRe3enZXlsgToicC8cmvcb3a8Z92pkgjfW3gTC7kKia8jbSJKzf9kiapB643jvRUhpDTe4II5lIc4A/0';
					}
					retUrl = '<a href="javascript:void(0)">'
						+ '<div style="display:table;text-align:center;height:80px;width:100%">'
						+ '<span style="display:table-cell;vertical-align:middle">'
						+ '<img src="' +value+'?x-oss-process=image/resize,m_fixed,h_60,w_100'
						+ '" style="display:inline;max-height:80px;max-width:110px"></span></div></a>';
					return retUrl;
				}
			},
	        {field:'design_id',title:'<font color="red">模板ID</font>',width:25,align:'center',sortable:false}, 
	        {field:'design_name',title:'模板名称',width:70,align:'center',sortable:true},
	        {field:'user_id',title:'用户ID',width:30,align:'center',sortable:true},
	        {field:'user_name',title:'设计师',width:50,align:'center',sortable:true},
//	        {field:'publish',title:'发布状态',width:30,align:'center',sortable:true,
//	        	formatter:function(publish, rowData, rowIndex){
//	        		if (Number(publish) == 1) return '<span style="color:green">已发布</span>';
//	        		return '<span style="color:red">未发布</span>';
//	        	}
//	        },
	        {field:'vip',title:'VIP',width:40,align:'center',sortable:true,
	        	formatter:function(value, rowData, rowIndex){
		    		  var show = '普通模版';
		    		  if(Number(value||'0')==10) show="<span style=''>VIP模版</span>";
		    		  return show;
		    	 }
	        },
	        {field:'order',title:'排序',width:30,align:'center',sortable:true},
	        {field:'state',title:'状态',width:30,align:'center',sortable:true,
	        	formatter:function(state, rowData, rowIndex){
	        		var s = Number(state||'0');
	        		var nm = '未知';
	        		switch(s){
	        			case 10: nm='<span style="color:#8B6914">制作中</span>';break;
	        			case 20: nm='<span style="color:blue">待审核</span>';break;
	        			case 31: nm='<span style="color:orange">初审OK</span>';break;
	        			case 41: nm='<span style="color:red">初审NG</span>';break;
	        			case 32: nm='<span style="color:green">复审OK</span>';break;
	        			case 42: nm='<span style="color:red">复审NG</span>';break;
	        		}
	        		return nm;
	        	}
	        },
	        {field:'showData',title:'DATA数据',width:30,align:'center',sortable:true,
	        	formatter:function(value,rowData,rowIndex){ 	// c.tmpl_json, c.tmpl_set
	        		return '<a href="javascript:void(0)" onclick="showData('+rowData.design_id+')">查看</a>';
	        	}
	        },
	        {field:'create_time',title:'创建时间',width:70,align:'center',sortable:true},
	        {field:'update_time',title:'修改时间',width:70,align:'center',sortable:true}
	    ]],
	    pagination:true,
		rownumbers:true
	});
}

function showData(designId){
	$("#tgDataTxt").val('');
	$("#tgFineDataTxt").val('');
	$("#tgAnijson").val('');
	$.loading(true);
	var url = appPath + "/yjxCust/getOneVideoDataInfo.action?designId="+designId;// 请求获取tmplJson数据
	$.post(url,function(ret){
		if (ret){
			$.loading(false);
			var info = ret[0];
			$("#tgDataTxt").val(info.tg_data);
			$("#tgFineDataTxt").val(info.v_data);
			$("#tgAnijson").val(info.ani_json);
		} else {
			$.loading(false);
		}
	});
	
	$('#tempData').window({
		title:'视频模板DATA数据信息',
		iconCls:'icon-tip',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	$('#tempData').window('move',{top:20,left:200});
	$('#tempData').window('open');
	
	
	$('#tt').tabs({
	    border:false,
	    onSelect:function(title){
			
	    }
	});
}





// 判断是否是JSON文件
function isJSON(str) {
    if (typeof str == 'string') {
        try {
            var obj=JSON.parse(str); // 从字符串中解析出 json对象
            if(str.indexOf('{')>-1){
                return true;
            }else{
                return false;
            }

        } catch(e) {
            console.log(e);
            return false;
        }
    }
    return false;
}

function getQueryParam(){
	var queryObject = {};
	var findVname = $("#queryVName").val();// 作品名称
	var findVdesignId = $("#queryVDesignId").val();// 作品ID
	if(findVname && findVname.length>0) queryObject.findVname = findVname;// 
	if (Number(findVdesignId)>0) queryObject.findVdesignId = findVdesignId;
	return queryObject;
}

/*  查询数据
 */
function doQuery(){
	$("#videoTmplGrid").datagrid('unselectAll');
	$("#videoTmplGrid").datagrid('load',getQueryParam());
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.create_time=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}
